package com.test0109;

import java.util.*;

public class Test07 {
    public static void main(String[] args) {
        // (7)list集合按照要求存储到map中

        M m1 = new M("张三",30);
        M m2 = new M("李四",40);
        M m3 = new M("王五",50);

        List<M> list = new ArrayList<M>();

        list.add(m1);
        list.add(m2);
        list.add(m3);


        Map<String, M> map1 = new TreeMap<>();
        Map<Integer, M> map2 = new TreeMap<>();
        for(M m:list){
            map1.put(m.getName(),m);
            map2.put(m.getAge(),m);
        }
        System.out.println("map要求一");
        map1.forEach((k,v)->{
            System.out.println(k+"\t\t"+v);
        });
        System.out.println("map要求二");
        map2.forEach((k,v)->{
            System.out.println(k+"\t\t"+v);
        });

        // map要求一的结构
        // key      vaule
        // 张三 ->   m1对象
        // 李四 ->   m2对象
        // 王五 ->   m3对象

        // map要求二的结构
        // key      vaule
        // 30 ->   m1对象
        // 40 ->   m2对象
        // 50 ->   m3对象


    }
}
class M{
    private String name;
    private int age;

    public M() {
    }

    public M(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "M{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
